package com.itheima.filterConfig;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class Demo8Filter implements Filter {
	FilterConfig fc = null;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		fc=filterConfig;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("Demo8Servlet 过滤到请求");
		//获取filter的名称
		String filterName = fc.getFilterName();
		System.out.println(filterName);
		//获取filter的初始化参数
		String db = fc.getInitParameter("db");
		System.out.println(db);
		//获取filter所有的初始化名称
		Enumeration<String> initParameterNames = fc.getInitParameterNames();
		while (initParameterNames.hasMoreElements()) {
			String string = (String) initParameterNames.nextElement();
			System.out.println(string+":"+fc.getInitParameter(string));
			
		}
		//获取上下文对象(ServletContext)
		ServletContext sc = fc.getServletContext();
		System.out.println(sc);
		chain.doFilter(request, response);
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
